/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <algorithm>

#include "EventServer.h"

using namespace Chronos;

void EventServer::generateEvent(Event::EventType event, Session& session) {
    EventListenersConstIter iter = eventListeners.find(event);
    if (iter != eventListeners.end()) {
        const Listeners &l = iter->second;
        for (ListenersConstIter iter2 = l.begin(); iter2 != l.end(); ++iter2) {
            (*iter2)->notify(event, session);
        }
    }
}


void EventServer::registerListener(Event::EventType event, EventListener* listener) {
    Listeners &l = eventListeners[event];
    ListenersConstIter iter2 = find(l.begin(), l.end(), listener);
    if (iter2 == l.end()) {
        l.push_back(listener);
    }
}


void EventServer::unregisterListener(const EventListener* listener) {
    for (EventListenersIter iter = eventListeners.begin(); iter != eventListeners.end(); ++iter) {
        Listeners &l = iter->second;
        ListenersIter iter2 = find(l.begin(), l.end(), listener);
        if (iter2 != l.end()) {
            l.erase(iter2);
            continue;
        }
    }
}

